from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from DButils import Mysql
from Test_login import Loginchecker
import time

class Usermanagechecker:
    def __init__(self,username,password):
        self.username = username
        self.password = password

    def test_usermanage(self):
        un = self.username
        pw = self.password

        #创建一个谷歌浏览器对象
        a = webdriver.Chrome()
        try:
            # 打开系统网址
            a.get("http://localhost:90")

            # 窗口最大化
            a.maximize_window()

            WebDriverWait(a, 10, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME, 'lear-login')))

            # 输入用户名，密码
            a.find_element(By.ID, 'username').send_keys(f'{un}')
            a.find_element(By.NAME, 'password').send_keys(f'{pw}')

            # 点击登录
            a.find_element(By.XPATH, '//*[@id="login_btn"]').click()

            # 等待显示系统首页
            # WebDriverWait(a, 10, 0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a')))
            WebDriverWait(a, 10, 0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a')))
            #点击用户信息管理
            a.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
            #点击用户信息
            time.sleep(1)
            a.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
            #点击新增按钮
            a.find_element(By.XPATH,'//*[@id="user_add_modal_btn"]').click()
            #强制等待1秒
            time.sleep(1)
            #填写用户名密码
            a.find_element(By.CSS_SELECTOR,'#username_add_input').send_keys("小强")
            a.find_element(By.CSS_SELECTOR,'#password_add_input').send_keys("123456")
            #点击保存按钮
            a.find_element(By.CSS_SELECTOR,'#user_save_btn').click()

            #强制等待三秒
            time.sleep(3)
        except Exception as e:
            print("新增用户失败！",e)
        finally:
            a.quit()

    def verify_abc(self):
        un = self.username
        pw = self.password
        db1 = Mysql("127.0.0.1", 3306, "root", "root", "finance", "utf8")
        sql_s1 = f"select * from bankcard where cardNum = '{un}' and cardBank = '{pw}'"
        result = db1.select(sql_s1)
        if result:
            print("新增用户成功！")
        else:
            print("新增用户失败！")

if __name__ == '__main__':
    l = Usermanagechecker('admin', '123456')
    l.test_usermanage()
    l.verify_abc()



